<template>
	<view class="u-p-l-20">
		<u-form :model="form" ref="uForm" :error-type="errorType">
			<u-form-item label="昵称">
				<u-input v-model="form.name" placeholder="请输入昵称" />
			</u-form-item>
			<u-form-item label="头像">
				<!-- <oss-upload @upSuccess="upSuccess"></oss-upload> -->
				<fastdfs-upload @upSuccess="upSuccess"></fastdfs-upload>
			</u-form-item>
		</u-form>
		<view class="u-flex-1 u-m-t-20">
			<u-button @click="submit" :ripple="true" type="primary" shape="square" size="medium">提交</u-button>
		</view>
        <!--#ifdef APP-PLUS-->
		<view v-if="vuex_user.email === '123@qq.com'">
			<view>语音转文字测试页</view>
			<view>你在说:{{voicetext}}</view>
			<button class="uni-btn" type="warn" @touchstart="startvoice" @touchend="endvoice">按住语话松开停止</button>
			<button class="uni-btn" type="warn" @tap="playvoice">播放录音</button>
		</view>
		<!--#endif-->
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	export default {
		onShow() {
			// 拿到用户vuex的信息
			this.form.name = this.vuex_user.name
		},
		onLoad() {
			// #ifdef APP-PLUS
			this.initaudio()
			// #endif
		},
		data() {
			return {
				form: {
					name: '',
					avatar: ''
				},
				// 文字提示
				errorType: ['message'],
				voicetext: '',
				msg: '',
				voicepath: ''
			};
		},
		methods: {
			// 节流
			submit() {
				this.$u.throttle(this.submitNo(), 3000)
			},
			// 拿到子组件上传图片成功事件
			upSuccess(data) {
				// 图片上传成功后 保存图片key
				this.form.avatar = data
				console.log('baseInfo upSuccess', data)
			},
			// 提交
			async submitNo() {
				// 更新昵称
				if (this.form.name) {
					// 准备参数
					const params = {
						name: this.form.name
					}
					// 调用更新用户信息接口
					await this.$u.api.setUserInfo(params)

					// 更新用户信息
					this.$u.utils.setUserInfo()
					this.$u.toast("更新信息成功")
				}
				// 更新头像
				if (this.form.avatar) {
					const params = {
						avatar: this.form.avatar
					}
					// 发送更改头像请求
					await this.$u.api.authAvatar(params)
					// 更新缓存数据
					this.$u.utils.setUserInfo()
				}
			},

			//语音转文字测试
			async initaudio() {
				//注意此处必须为 await 因为会触发异步事件，手机上会弹出权限申请对话框处理完才能走下一步录音  
				let recordauth = await permision.requestAndroidPermission("android.permission.RECORD_AUDIO")
				console.log("判断有没有录音权限>>>>>>" + recordauth)
				if (recordauth == 1) {
					recorderManager.onStart((res) => {
						console.log("开始 录音>>>>>>>>>...")
					});
					recorderManager.onStop((res) => {
						console.log("recorderstop....res.tempFilePath>>>" + res.tempFilePath)
						this.voicepath = res.tempFilePath
						this.uploadvoicefile()
						// 使用uni.uploadFile上传到服务器上，此时是mp3格式  
					});

					recorderManager.onError((res) => {

						console.log('onError' + JSON.stringify(res));
					});
				}
			},
			//initaudio 方法结束  
			startvoice() {
				console.log("开始录音")
				recorderManager.start({
					format: "mp3",
					sampleRate: 16000 // 必须设置是后台设置的参数，不然百度语音识别不了  
				});
			},
			endvoice() {
				console.log("结束录音")
				//注意为了避免说话时间太短导致这个api出现bug要加一些延时  
				setTimeout(() => {
					recorderManager.stop()
				}, 1000)

			},
			playvoice() {
				console.log("点击playvoice")
				if (this.voicepath) {
					console.log("播放声音")
					innerAudioContext.src = this.voicepath;
					innerAudioContext.play();
				}
			},
			uploadvoicefile() {
				// this.msg = "调用java端服务文件路径"+this.voicepath  
				uni.uploadFile({
					url: 'http://124.221.192.169:996/emos-wx-api/uploadFile',
					filePath: this.voicepath, //录音结束后返回的临时路径，  
					name: 'file',
					formData: {
						dev_id: 1537 //中文带标点  
					},
					success: (uploadFileRes) => {
						//console.log(uploadFileRes.data)
						const result = JSON.parse(uploadFileRes.data)
						//console.log(result)
						this.voicetext = result.msg
						uni.showToast({
							icon: 'none',
							title: this.voicetext,
							duration: 3000
						})
						console.log("上传音频成功" + this.voicetext);
					},
					fail: (res) => {

						console.log("上传音频失败" + JSON.stringify(res));
					}
				});
			}
		}
	}
</script>

<style lang="scss">

</style>
